<template>
  <el-dialog :title="!form.id ? $t('commonAdd') : $t('commonEdit')" :visible.sync="visible" :close-on-click-modal="false" width="40%" top="10vh">
    <div class="sketch_content">
      <el-form :model="form" label-width="120px" ref="dataForm">
        <el-form-item :label="$t('faq.question')" prop="question" :rules="rules.isRange('1-255')">
          <el-input v-model="form.question" :placeholder="$t('commonInput') + $t('faq.question')" maxlength="255" show-word-limit/>
        </el-form-item>
        <el-form-item :label="$t('faq.type')" prop="type" :rules="rules.isNotNull">
          <el-radio-group v-model="form.type">
            <el-radio :label="1">{{$t('faq.type1')}}</el-radio>
            <el-radio :label="2">{{$t('faq.type2')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('faq.orderNum')" prop="orderNum" :rules="rules.isInteger">
          <el-input v-model="form.orderNum" :placeholder="$t('commonInput') + $t('faq.orderNum')"/>
        </el-form-item>
        <el-form-item :label="$t('faq.remark')" prop="remark">
          <el-input v-model="form.remark" :placeholder="$t('commonInput') + $t('faq.remark')" maxlength="64" show-word-limit/>
        </el-form-item>
        <el-form-item :label="$t('faq.answer')">
          <div id="answerEditor"></div>
        </el-form-item>
      </el-form>
    </div>

    <span slot="footer" class="dialog-footer">
      <el-button size="small" icon="el-icon-close" @click.native="visible = false">{{$t('commonCancel')}}</el-button>
      <el-button size="small" icon="el-icon-check" type="primary" :loading="isDisabled"
        @click.native="saveOrUpdateForm(form, '/faq/save', '/faq/update')">{{$t('commonConfirm')}}
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        rules: this.getRules(),
        isDisabled: false,
        answerEditor: '',
        form: {
          question: '',
          answer: '',
          orderNum: '',
          remark: '',
          tenantId: '',
          type: 1,
          flag: 1
        }
      }
    },
    methods: {
      saveOrUpdateForm (form, saveUrl, updateUrl) {
        form['answer'] = this.answerEditor.txt.html()
        this.saveOrUpdate(form, saveUrl, updateUrl)
      },
      init (id) {
        this.visible = true
        this.isDisabled = false
        this.$nextTick(() => {
          this.answerEditor = this.initMultiText('#answerEditor', this.answerEditor)
          this.answerEditor.create()
          this.$refs['dataForm'].resetFields()
        })
        this.form.id = id
        if (id) {
          this.httpGet(`/faq/info/${id}`).then(res => {
            this.form = res.data
            this.answerEditor.txt.html(this.form.answer)
          })
        } else {
        }
      }
    }
  }
</script>

